import { loginAPI } from '@/api'
import { getToken, removeToken, setToken } from '@/utils/auth'
export default {
  state: {
    token: getToken()// 初始化的时候从本地缓存读取
  },
  mutations: {
    setToken(state, token) {
      state.token = token
      // 同步到持久化
      setToken(token)
    },
    removeToken(state) {
      state.token = null
      removeToken() // 删除本地缓存的token
    }
  },
  actions: {
    // store是框架层传入的
    async login(store, data) {
      // const token = await loginAPI(data)
      // store.commit('setToken', token)
      store.commit('setToken', await loginAPI(data)) // 设置token
    }
  },
  namespaced: true
}

